import traceback

try:
    data = {"key": "value"}
    print(data["nonexistent_key"])
except Exception as e:
    print("错误信息:", str(e))
    print("错误类型:", type(e).__name__)
    print("追踪信息:")
    traceback.print_exc()  # 直接打印栈信息

    # 获取详细的堆栈信息
    tb_info = traceback.format_exc()
    print("格式化堆栈:")
    print(tb_info)
